﻿@{
	Layout = "";

	var dto = ViewBag.dto as Datafeed.ResultGetPage;
}

<div class="box">
	<div class="box-header with-border">
		<h3 id="box-title" class="box-title"></h3> （@dto.Description <a href="./add?tpl=CleanStorageData">数据太多如何清理？</a>）
		<span class="form-group mr15"></span><a href="./add" data-toggle="modal" class="btn btn-success pull-right">添加</a>
	</div>
	<div class="box-body">
		<div class="table-responsive">
			<form id="form_search">
				<div id="div_filter"></div>
			</form>
			<form id="form_list" action="./del" method="post">
				<input type="hidden" name="__callback" value="del_callback"/>
				<table id="GridView1" cellspacing="0" rules="all" border="1" style="border-collapse:collapse;" class="table table-bordered table-hover text-nowrap">
					<tr>
						<th scope="col">操作</th>
						<th scope="col">编号</th>
						<th scope="col">标题</th>
						<th scope="col">定时</th>
						<th scope="col" class="text-right">轮次</th>
						<th scope="col">状态</th>
						<th scope="col">Body</th>
						<th scope="col">创建时间</th>
						<th scope="col">最后运行时间</th>
						<th scope="col">错次数</th>
					</tr>
					<tbody>
						@foreach(var item in dto.Tasks) {
							<tr>
								<td>
									<input type="button" class="btn btn-xs btn-danger" value="删除" onclick="if (confirm('确认删除吗？')) top.callTask('delete', '@item.Id')" />

									@if (item.Status != FreeScheduler.TaskStatus.Completed)
									{
										@if (item.Status == FreeScheduler.TaskStatus.Paused)
										{
											<input type="button" class="btn btn-xs btn-success" value="恢复" onclick="top.callTask('resume', '@item.Id')" />
										}
										@if (item.Status == FreeScheduler.TaskStatus.Running)
										{
											<input type="button" class="btn btn-xs btn-warning" value="暂停" onclick="top.callTask('pause', '@item.Id')" />
										}
										<input type="button" class="btn btn-xs btn-info" value="立即触发" onclick="top.callTask('run', '@item.Id')" />
									}
								</td>
								<td>@item.Id</td>
								<td>@item.Topic</td>
								<td>@item.Interval @item.IntervalArgument</td>
								<td class="text-right">
									@if (dto.Description.Contains("存储: Memory"))
									{
										@(item.CurrentRound + "/" + item.Round)
									}
									else
									{
										<a href="../TaskLog/?taskId=@item.Id">@(item.CurrentRound + "/" + item.Round)</a>
									}
								</td>
								<td>@item.Status</td>
								<td>@item.Body</td>
								<td>@item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
								<td>
									@if (dto.Description.Contains("存储: Memory"))
									{
										@item.LastRunTime.ToString("yyyy-MM-dd HH:mm:ss")
									}
									else
									{
										<a href="../TaskLog/?taskId=@item.Id">@item.LastRunTime.ToString("yyyy-MM-dd HH:mm:ss")</a>
									}
								</td>
								<td>@item.ErrorTimes</td>
                            </tr>
						}
					</tbody>
				</table>
			</form>
			<div id="kkpager"></div>
		</div>
	</div>
</div>

@{
	var timestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

	string getClusterText(int index, Datafeed.ResultGetPage.ClusterInfo cluster) 
	{
		var name = cluster.Name;
		if (string.IsNullOrWhiteSpace(name)) name = cluster.Id;
		if (timestamp - cluster.Heartbeat < 10) return $"{name}({cluster.TaskCount})";
		if (timestamp - cluster.Heartbeat < 30) return $"<font color=red>{name}({cluster.TaskCount})[可能离线]</font>";
		return $"<font color=red>{name}({cluster.TaskCount})[离线]</font>";
	}
}

<script type="text/javascript">
	(function () {
		top.callTask = function(opt, id) {
			$.ajax({
				type: 'GET',
				url: top.mainViewNav.trans('./callTask'),
				data: { id: id, opt: opt },
				dataType: 'json',
				success: (rt) => {
					console.log(rt);
					setTimeout(top.mainViewNav.reload, 1000);
				},
			});
		}

		top.del_callback = function(rt) {
			if (rt.code == 0) return top.mainViewNav.goto('./?' + new Date().getTime());
			alert(rt.message);
		};

		var qs = _clone(top.mainViewNav.query);
		var page = cint(qs.page, 1);
		delete qs.page;
		$('#kkpager').html(cms2Pager(@dto.Total, page, 20, qs, 'page'));
		var fqs = _clone(top.mainViewNav.query);
		delete fqs.page;
		var fsc = [
			{ name: '集群', field: 'clusterId', single: 1, text: @Html.Json(dto.Clusters?.Select((a, index) => getClusterText(index, a))), value: @Html.Json(dto.Clusters?.Select(a => a.Id)) },
			{ name: '状态', field: 'status', single: 1, text: ['运行中','暂停中','已完成'], value: [0,1,2] },
			null
		];
		fsc.pop();
		cms2Filter(fsc, fqs);
		top.mainViewInit();
	})();
</script>
